<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="维修时间">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                            type="daterange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="维修次数" prop="repairCount"/>
      <el-table-column align="center" label="维修总成本" prop="repairMoney">
        <template slot-scope="scope">
          {{
            toMoney((scope.row.fittingMoney ? scope.row.fittingMoney : 0) + (scope.row.repairMoney ?
              scope.row.repairMoney : 0))
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button  size="mini" type="text"
                     @click="handleRepairDetails(scope.row)">维修明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 维修记录 -->
    <el-dialog :title="title" :visible.sync="detailsOpen" append-to-body width="80%">
      <el-table v-loading="loading" :data="detailsList">
        <el-table-column align="center" label="报修人" prop="reportUserName"/>
        <el-table-column align="center" label="报修时间" prop="reportTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="报修描述" prop="reportDesc"/>
        <el-table-column align="center" label="配件金额" prop="fittingMoney">
          <template slot-scope="scope">
            {{ toMoney(scope.row.fittingMoney) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="其他金额" prop="repairMoney">
          <template slot-scope="scope">
            {{ toMoney(scope.row.repairMoney) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="维修合计">
          <template slot-scope="scope">
            {{
              toMoney((scope.row.fittingMoney ? scope.row.fittingMoney : 0) + (scope.row.repairMoney ?
                scope.row.repairMoney : 0))
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="维修人" prop="repairUserName"/>
        <el-table-column align="center" label="损坏状态" prop="repairStatus">
          <template slot-scope="scope">
            <dict-tag :options="repairStatusOptions" :value="parseInt(scope.row.repairStatus)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="维修时间" prop="repairTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="维修描述" prop="repairDesc"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-button  size="mini" type="text"
                       @click="handleConfirmRepairOpen(scope.row, 2)">查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="viewOpen" append-to-body title="详情" width="60%">
      <view-repair :form="form"></view-repair>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRepairDetails, getRepairDetailsList, getRepairGroupProjectList} from "@/api/cld/record";
import {repairStatusOptions} from '@/assets/staticDict'
import viewRepair from "./viewRepair.vue";

export default {
  name: "repair",
  dicts: ['cld_assets_type', 'cld_project_region'],
  components: {viewRepair},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      viewOpen: false,
      detailsOpen: false,
      // 资产维修表格数据
      list: [],
      detailsList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        assetsDetailsId: this.$route.params.assetsDetailsId
      },
      form: {},
      repairStatusOptions: repairStatusOptions
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产维修列表 */
    getList() {
      this.loading = true;
      getRepairGroupProjectList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 维修明细
    handleRepairDetails(row) {
      getRepairDetailsList({
        pageNum: 1,
        pageSize: 10,
        projectId: row.projectId,
        assetsDetailsId: row.assetsDetailsId
      }).then(response => {
        this.detailsList = response.rows;
        this.title = row.assetsName + '-' + row.projectName + '-维修记录'
        this.detailsOpen = true;
      });
    },
    // 查看详情
    handleConfirmRepairOpen(row) {
      getRepairDetails(row.assetsRecordId).then(res => {
        this.form = res.data;
        this.form.reportImgsList = this.form.repairImgs ? this.form.repairImgs.split(',') : [];
        this.form.repairImgsList = this.form.repairImgs ? this.form.repairImgs.split(',') : [];
        this.viewOpen = true;
      })
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
